package com.ls.uav_backend.service.impl;

import com.ls.uav_backend.mapper.PictureMapper;
import com.ls.uav_backend.model.entity.Picture;
import com.ls.uav_backend.service.PictureService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 拍摄图片 服务层实现。
 *
 * @author <a href="https://gitee.com/Linkating">LinSkating</a>
 */
@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements PictureService {

    @Override
    public List<Integer> getFieldIdList() {
        List<Integer> list = this.queryChain()
                .select(Picture::getFieldId)
                .list()
                .stream()
                .map(Picture::getFieldId)
                .distinct()
                .toList();
        return list;
    }

    @Override
    public List<String> getCreateTimeList() {
        // 定义日期格式化器（yyyy-MM-dd）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return this.queryChain()
                .select(Picture::getCreateTime) // 查询出LocalDateTime类型的createTime
                .list() // 获取List<LocalDateTime>
                .stream()
                // 将LocalDateTime格式化为yyyy-MM-dd字符串
                .map(picture -> picture.getCreateTime().format(formatter))
                .distinct() // 去重
                .toList()
                .reversed();
    }

    @Override
    public Map<Integer, List<String>> getPictureList(LocalDateTime createTime) {
        // 1. 计算当天起始时间（00:00:00）
        LocalDateTime startTime = createTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
        // 2. 计算当天结束时间（23:59:59.999...）
        LocalDateTime endTime = createTime.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
        List<Picture> list = this.queryChain()
                .select(Picture::getFieldId, Picture::getUrl)
                .between(Picture::getCreateTime, startTime, endTime)
                .list();
        return list.stream().collect(Collectors.groupingBy(
                Picture::getFieldId,  // 分组的键：fieldId
                Collectors.mapping(
                        Picture::getUrl,  // 提取 url 字段
                        Collectors.toList()  // 收集为 List<String>
                )
        ));
    }

    @Override
    public Map<Integer, List<String>> getPictureList(Integer fieldId, LocalDateTime createTime) {
        // 1. 计算当天起始时间（00:00:00）
        LocalDateTime startTime = createTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
        // 2. 计算当天结束时间（23:59:59.999...）
        LocalDateTime endTime = createTime.withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
        List<Picture> list = this.queryChain()
                .select(Picture::getFieldId, Picture::getUrl)
                .eq(Picture::getFieldId, fieldId)
                .between(Picture::getCreateTime, startTime, endTime)
                .list();
        return list.stream().collect(Collectors.groupingBy(
                Picture::getFieldId,  // 分组的键：fieldId
                Collectors.mapping(
                        Picture::getUrl,  // 提取 url 字段
                        Collectors.toList()  // 收集为 List<String>
                )
        ));
    }
}
